package tests;

import common.Utils;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class LoginPage extends Utils {
    public static String url = "http://8.137.19.140:9090/blog_login.html";
    public LoginPage(){
        super(url);
    }

    /**
     * 检查页面十分能正常打开
     */
    public void checkPageRight() throws IOException {
        //检查菜单
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));
        //检查登录框
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));

        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    /**
     * 登录成功--成功的账号和密码
     */
    public void LoginSuc() throws IOException {

        //先清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();


        //校验登录是否成功--注销按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 异常登录
     * ----用户名和密码都为空
     * ----用户名为空，密码不为空
     * ----用户名不为空，密码为空
     * ----正确的用户名。错误的密码
     * ----错误的用户，正确的密码
     * ----用户名和密码都错误
     * ----输入框：输入过长的内容，输入过短的内容、特殊字符...
     *
     * 正确的用户名，错误的密码
     */
    public void LoginFail() throws IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("1");

        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        driver.findElement(By.cssSelector("#submit")).click();



        //等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        //处理错误弹窗
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }


}





















